package cn.niit.controller;

import cn.niit.entity.Message;
import cn.niit.entity.query.MessageQuery;
import cn.niit.service.MessageService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;


@Controller
@RequestMapping("/message")
public class MessageController {
    @Resource
    private MessageService messageService;

    @GetMapping("/query")
    public String queryByMessage(Model model, MessageQuery messageQuery) {
        PageInfo<Message> messagePageInfo = messageService.queryByMessage(messageQuery);
        model.addAttribute("page", messagePageInfo);
        return "/message/message";
    }

    @GetMapping("/select")
    public String select(Model model, MessageQuery messageQuery) {
        PageInfo<Message> messagePageInfo = messageService.select(messageQuery);
        model.addAttribute("page", messagePageInfo);
        return "/message/message_teacher";
    }

    @GetMapping("/queryByTeacherCode")
    public String queryByTeacherCode(Model model, MessageQuery messageQuery) {
        PageInfo<Message> messagePageInfo = messageService.queryByTeacherCode(messageQuery);
        model.addAttribute("page", messagePageInfo);
        return "/message/message_teacher";
    }

    @GetMapping("/queryByStudentCode")
    public String queryByStudentCode(Model model, MessageQuery messageQuery) {
        PageInfo<Message> messagePageInfo = messageService.queryByStudentCode(messageQuery);
        model.addAttribute("page", messagePageInfo);
        return "/message/message";
    }

    @GetMapping("/queryByTeacher")
    public String queryTeacher(Model model, MessageQuery messageQuery) {
        PageInfo<Message> messagePageInfo = messageService.selectNull(messageQuery);
        model.addAttribute("page", messagePageInfo);
        return "/message/message_teacher";
    }


    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable Integer id, Model model) {
        model.addAttribute("message", messageService.queryMessageById(id));
        return "/message/replymessage";
    }

    @PostMapping("/edit")
    public String edit(Message message) {
        MessageQuery messageQuery = new MessageQuery();
        Integer id = message.getId();
        messageQuery.setId(message.getId());
        PageInfo<Message> messagePageInfo = messageService.queryByTeacherCode(messageQuery);
        boolean b;
        if (id != null) {
            b = messageService.editMessage(message);
        } else {
            b = messageService.addMessage(message);
        }
        return  "redirect:/message/query";
    }
    @PostMapping("/reply")
    public String reply(Message message) {
        MessageQuery messageQuery = new MessageQuery();
        Integer id = message.getId();
        messageQuery.setId(message.getId());
        PageInfo<Message> messagePageInfo = messageService.queryByTeacherCode(messageQuery);
        boolean b;
        if (id != null) {
            b = messageService.editMessage(message);
        } else {
            b = messageService.addMessage(message);
        }
        return "redirect:/message/queryByTeacher";
    }
    @GetMapping("/update")
    public String update(Model model) {
        Message message = new Message();
        model.addAttribute("message", message);
        return "/message/addmessage";
    }

    @GetMapping("/delete/{id}")
    public String deleteMessageById(@PathVariable("id") Integer id) {
        boolean b = messageService.deleteMessageById(id);
        return "redirect:/message/queryByTeacher";
    }
}